<template>
    <div>
         <div class="formContain">
            <titlebar :title-left='titleLeft1'></titlebar>
            <ul>
                <li class="flex">
                    <span class="label"><i class="require" v-if="isFirstTask == '00901'">*</i>开始时间</span>
                    <div v-if="isFirstTask == '00900'">
                        <span>{{datas.startTime}}</span>
                    </div>
                    <div @click="showdatepicker($event,'startTime')" v-if="isFirstTask == '00901'">
                      <input type="text"  id="begin" placeholder="请选择时间" :value="datas.startTime"  readonly disabled>
                      <img src="@/assets/images/img/calendarIcon.png" alt="" srcset="" >
                    </div>
                </li>
                 <li class="flex">
                    <span class="label"><i class="require" v-if="isFirstTask == '00901'">*</i>结束时间</span>
                    <div v-if="isFirstTask == '00900'">
                        <span>{{datas.endTime}}</span>
                    </div>
                     <div @click="showdatepicker($event,'endTime')"  v-if="isFirstTask == '00901'">
                       <input type="text"  id="end" placeholder="请选择时间" :value="datas.endTime"  readonly disabled>
                       <img src="@/assets/images/img/calendarIcon.png" alt="" srcset="" >
                     </div>
                </li>
                  <li class="flex">
                     <span class="label"><i class="require" v-if="isFirstTask == '00901'">*</i>调休类型</span>
                    <div v-if="isFirstTask == '00900'">
                        <span>{{datas.offTypeName}}</span>
                    </div>
                    <div @click="showpicker($event,'offTypeName','调休类型')"  v-if="isFirstTask == '00901'">
                      <input type="text"  id="typeName" placeholder="请选择调休类型" :value="datas.offTypeName"  readonly disabled>
                      <span class="iconfont icon-xiala"></span>
                    </div>
                </li>
                 <li class="flex">
                    <span class="label"><i class="require" v-if="isFirstTask == '00901'">*</i>调休时长（小时）</span>
                    <div v-if="isFirstTask == '00900'">
                        <span>{{datas.offAmount}}</span>
                    </div>
                     <div v-if="isFirstTask == '00901'">
                       <div class="van-cell van-field number-input">
                         <div class="van-cell__value van-cell__value--alone">
                           <div class="van-field__body">
                             <MyNumberInput :point="1" :max="999999" placeholder="请输入调休时长" v-model.number="datas.offAmount"></MyNumberInput>
                           </div>
                         </div>
                       </div>
                       <!--<van-field v-model="datas.offAmount" maxlength="6" @input="checkNum" placeholder="请输入调休时长" type="number" step="0.1" input-align="right"/>-->
                     </div>
                </li>
                <li class="flex">
                    <span class="label"><i class="require" v-if="isFirstTask == '00901'"></i>剩余调休时长（小时）</span>
                    <div  v-if="isFirstTask == '00900'">
                        <span>{{datas.offAmountRemain}}</span>
                    </div>
                    <div v-if="isFirstTask == '00901'">
                      <div class="van-cell van-field number-input">
                        <div class="van-cell__value van-cell__value--alone">
                          <div class="van-field__body">
                            <MyNumberInput :point="1" :max="999999" disabled placeholder="系统自动带出" v-model.number="datas.offAmountRemain"></MyNumberInput>
                          </div>
                        </div>
                      </div>
                      <!--<van-field v-model="datas.offAmountRemain" disabled placeholder="系统自动带出" type="number" step="0.1"  input-align="right"/>-->
                    </div>
                </li>
            </ul>

        </div>
         <div class="formContain">
            <titlebar :title-left='titleLeft2'></titlebar>
            <ul>
                <li class="flex">
                    <div class='full_width' style="min-height:2rem" v-if="isFirstTask == '00900'">
                        <van-field class='full_width' :value="datas.remark" rows="1" autosize readonly="readonly" type="textarea" input-align="left"/>
                    </div>
                    <div class='full_width' style="min-height:2rem" v-if="isFirstTask == '00901'">
                      <i class="require">*</i>
                      <van-field class='full_width'  rows="1" autosize v-model="datas.remark" placeholder="请输入调休原因" type="textarea" input-align="left"/>
                    </div>
                </li>
              <li class="flex" v-if="isFirstTask == '00900'">
                <span class="label">预览附件</span>
                <div style="flex-grow: 1">
                  <!-- <input class="file" type="file"  id="type"  readonly> -->
                  <input type="hidden" v-model="datas.attachmentId">
                  <input type="text" style="width: 10rem;background: #fff" v-model="fileName" disabled>
                  <span class="iconfont icon-yulan" style="font-size: 1rem" @click="downFile"></span>
                </div>
              </li>
              <li class="flex"  v-if="isFirstTask == '00901'">
                <span class="label">上传附件</span>
                <div>
                  <!-- <input class="file" type="file"  id="file"  readonly>-->
                  <input type="text" v-model="fileName" disabled style="background: transparent;width: 10rem;position: absolute;padding-right: 1rem">
                  <van-uploader :after-read="afterRead" :before-read="beforeRead" id="file">
                    <span class="iconfont icon-hao"></span>
                  </van-uploader>
                </div>
              </li>
              <van-image-preview
                v-model="show1"
                :images="images"
              >
              </van-image-preview>
            </ul>
         </div>
      <van-picker id="picker"   show-toolbar  :title="title" v-show='pickershow' :columns="breakOffColumns" @cancel="onCancel" @confirm="onConfirm" />
      <van-datetime-picker v-show='show' id="datetime-picker" type="datetime" @confirm="datetimeSelect" @cancel='cancel' v-model="currentTime" />
    </div>
</template>

<script>
    import headTop from '@/components/header/head'
     import titlebar from '@/components/titlebar3/titlebar3';
    import {actdata,uploadFile,downloadFile,downloadFileType,breakOffType,breakOffRemain} from '@/service/getData';
    import {getStore,getDefault} from '@/assets/config/mUtils';
    import { Dialog,Toast} from 'vant';
    import MyNumberInput from '@/components/numberInput/numberInput';
    export default {
    	data(){
            return{
                titleLeft1:'调休信息',
                titleLeft2:'调休事由',
                show:false, //控制日期显示隐藏
                pickershow:false,//控制下拉选显示隐藏
                pickerName:'',//当前控制日期的控件
                currentTime:new Date(),
                fileName:'',
                screenHeight:'',
                images:[],
                show1: false,
                datas:{
                  endTime:'',
                  startTime:'',
                  offTypeName:'',
                  offAmount:'',
                  offAmountRemain:0,
                  remark:'',
                  attachmentId:''
                },
                title:'标题',//下拉框标题
                breakOffColumns: []  //下拉框数据
            }
        },
       props:['isFirstTask','procInstanceId'],
        mounted(){
          this.screenHeight = document.documentElement.clientHeight;
          this.$nextTick(function () {
            var defaultData = getDefault();
            breakOffType(defaultData).then(res=>{
              this.breakOffColumns = res.data["3225"];
              var actData = {};
              actData.procInstanceId = this.procInstanceId;
              actData.app_userId = getStore('user_id');
              actData.CHECK_TIME = new Date().getTime();
              actData.CHECK_SIGN = this.$md5(actData.app_userId + actData.CHECK_TIME +"3a28fba00c015e0df7f88556c91cb7fc");
              actdata(actData).then(res=>{
                console.log(res,"222222222222");
                var resData = res.data.datas[0];
                for(var i=0;i<this.breakOffColumns.length;i++){
                  var curColumn = this.breakOffColumns[i];
                  if(curColumn.id == resData.offType){
                    resData.offTypeName = curColumn.text;
                  }
                };
                this.datas = resData;
                var data = {};
                data.fileId = this.datas.attachmentId||"";
                //data.fileId = '08ed9a58d7184c1898def154c05be2ec';
                downloadFileType(data).then(res=>{
                  this.fileName = res.data;
                })
              })
            });
          });
        },
        updated () {
        let that = this;
        this.$nextTick(function () {
          let $inputs = Array.from(document.getElementsByTagName('input'));
          /*let body = document.body;
          $inputs.forEach(item => { // 定时器是处理多个input间快速切换闪屏问题，可省略
              item.onblur = function () { // onblur是核心方法
                  clearTimeout(body.timer);
                  body.timer = setTimeout(() => {
                  window.scroll(0, 0);
                  window.innerHeight = window.outerHeight = that.screenHeight
                  }, 150)
              }
              item.onfocus = function () {
                  clearTimeout(body.timer)
              }
          })*/
          let body = document.body;
          $inputs.forEach(item => {
            item.onblur = function () { // onblur是核心方法
              window.scroll(0, 0);
              window.innerHeight = window.outerHeight = that.screenHeight;
              //console.log(that.screenHeight);
            }
          })
        })
      },
        components:{
            headTop,
            titlebar,
          MyNumberInput
        },
        watch:{
          datas:{
            handler: function (newVal,oldVal) {
              this.$emit('childactdata',this.datas)
            },
            deep: true
          }
        },
        methods:{
          // checkNum(){
          //   this.datas.offAmount = parseFloat(this.datas.offAmount)
          // },
            addZero(num){
              var addNum = num<10?"0"+num:num;
              return addNum;
            },
            datetimeSelect(value){
                this.show = false;
              var date =  value;
              var val = date.getFullYear() + '-'+this.addZero(date.getMonth()+1) + '-'+ this.addZero(date.getDate())+' '+this.addZero(date.getHours()) + ':'+ this.addZero(date.getMinutes())+':'+"00";
              this.$set(this.datas,this.pickerName,val);
              // document.getElementById(this.pickerName).value = val;
              if(this.datas.startTime!=''){
                var breakData = {};
                breakData.personId = getStore('user_id');
                breakData.startTime = this.datas.startTime;
                breakData.app_userId = getStore('user_id');
                breakData.CHECK_TIME = new Date().getTime().toString();
                breakData.CHECK_SIGN = this.$md5(breakData.app_userId + breakData.CHECK_TIME +"3a28fba00c015e0df7f88556c91cb7fc");
                breakOffRemain(breakData).then(res=>{
                  this.datas.offAmountRemain = res.data.data;
                })
              };
              if(this.datas.endTime != ""){
                if(this.datas.startTime >= this.datas.endTime){
                  Dialog.alert({
                    title: '',
                    message: '开始时间不能大于结束时间'
                  });
                  this.datas.endTime = '';
                }
              };
            },
            onConfirm(value){
                this.$set(this.datas,this.pickerName,value.text);
                 this.pickershow = false;
            },
            cancel() {
                this.show = false;
            },
            onCancel(){
                this.pickershow = false;
            },
            showdatepicker(event,name){
                this.show = true;
                this.pickerName = name;
            },
            showpicker(event,name,title){
                this.pickershow = true;
                this.pickerName = name;
                this.title = title;
            },
            beforeRead(file) {
              var imgType = file.type.toLocaleLowerCase();
              if (imgType != 'image/jpg'&&imgType != 'image/jpeg'&&imgType != 'image/png'&&imgType != 'image/gif'&&imgType != 'image/bmp') {
                Toast('请上传图片');
                return false;
              }

              return true;
            },
            afterRead(file) {
              // 此时可以自行将文件上传至服务器
              Toast.loading({
                duration: 0,   // 持续展示 toast
                mask: true,
                message: '正在上传...'
              });
              var formData = new FormData();
              formData.append("uploadFile", file.file);
              uploadFile(formData).then(res=>{
                this.fileName = file.file.name;
                this.datas.attachmentId = res.fileId;
                Toast.clear();
              })
            },
            downFile(){
              var data = {};
              //96c4cf9754b7492b91b53a8a44a5d74b    08ed9a58d7184c1898def154c05be2ec
              data.fileId = this.datas.attachmentId||"";
              data.app_userId = getStore('user_id');
              data.CHECK_TIME = new Date().getTime().toString();
              data.CHECK_SIGN = this.$md5(data.app_userId + data.CHECK_TIME +"3a28fba00c015e0df7f88556c91cb7fc");
              //判断有无附件
              downloadFileType(data).then(res=>{
                var fileType = res.data.split(".")[(res.data.split(".").length-1)];
                //console.log(fileType== "",res.code);
                if(fileType == "jpg" ||fileType == "png"||fileType == "jpeg" ||fileType == "gif"||fileType == "bmp"&&res.code =="200"){
                  downloadFile(data).then(res=>{
                    //var blob = new Blob(res);
                    if(this.images.length == 0){
                      this.images.push(res.data.imgurl)
                      //this.images.push(window.webkitURL.createObjectURL(res));
                      this.show1 = true;
                    }else {
                      this.show1 = true;
                    }
                  });
                }else if(res.code=="400"&& fileType== ""){
                  Dialog.alert({
                    title: '提示',
                    message: '暂无附件'
                  })
                }else {
                  Dialog.alert({
                    title: '提示',
                    message: '文件类型在移动端不支持预览，请在PC端进行查看'
                  })
                }
              });

            }
        }
    }

</script>

<style lang="scss" scoped>
      @import '../../../../assets/style/mixin';
      @import '../../../../assets/style/public';
    .formContain{
        background: #fff;
        >table{
            width: 100%;
            border-collapse: collapse;
            td{
                font-size: .56rem;
                color: #444;
                border-bottom: 1px solid #e0e0e0;
                line-height: 2rem;
                text-align: center;
                width: 33.3%;
            }
        }
        ul{
            padding: 0 .54rem;
            .flex{
                justify-content: space-between;
                align-items: center;
                line-height: 2rem;
                border-bottom: 1px solid #e0e0e0;
                padding: 0 .3rem;
                &:nth-last-of-type(1){
                    border: none;
                }
                .label{
                    font-size: .56rem;
                    line-height: 2rem;
                    color: #333333;
                }
                .opinion{
                    font-size: .56rem;
                    font-weight: normal;
                    font-stretch: normal;
                    line-height: 1.2rem;
                    letter-spacing: 0px;
                    color: #666666;
                    padding-bottom: .3rem;
                }
                div{
                    display: flex;
                    justify-content: flex-end;
                    align-items: center;
                    position: relative;
                    flex-grow: 1;
                    span{
                        font-size: .56rem;
                        color: #333;
                    }
                    img{
                        width: .68rem;
                        height: .64rem;
                    }
                    input{
                        color: #323233;
                        text-align: right;
                        font-size: .48rem;
                        padding-right: .48rem;
                        border: none;
                        background: #fff;
                        &:-ms-input-placeholder{
                             color: #aaa;
                             font-size: .48rem;

                        }
                    }
                    .file{
                        width: 2rem;
                        height: 1rem;
                        font-size: 10rem;
                        opacity: 0;
                        position: absolute;
                        right: 0;
                    }
                    .van-cell{
                        font-size: .48rem;
                        padding-right: .48rem;
                        padding: 0;
                        color: #aaa;
                        .van-field__control{
                            color: #aaa;
                        }
                    }

                }
            }
        }
    }
    .foot-button{
        margin-top: 2rem;
        margin-bottom: .4rem;
        .cancel-btn{
            margin-right: 1rem;
        }
    }
    #datetime-picker,#picker{
        position: fixed;
        width: 100%;
        bottom: 0;
        z-index: 9;
    }
      .require{
        color: red;
        margin-right: 4px;
        font-style: inherit;
        font-size: 14px;
      }
</style>
